package com.feidian.helper.ui;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.feidian.helper.R;
import com.feidian.helper.net.DownloadProgressListener;
import com.feidian.helper.net.FileDownloader;

public class DownloadActivity extends Activity implements OnClickListener {
	private   ImageView mBack;
	private   TextView mTitle;
	private   ProgressBar mProgressbar;
	private   TextView    mProgressPercent;
	private String mURL = "http://gdown.baidu.com/data/wisegame/3c00add7144d3915/kugouyinle.apk";
	private File mFile;
    private Handler handler = new Handler(){



        @Override

        public void handleMessage(Message msg) {            

            switch (msg.what) {

            case 1:                

                mProgressbar.setProgress(msg.getData().getInt("size"));

                float num = (float)mProgressbar.getProgress()/(float)mProgressbar.getMax();

                int result = (int)(num*100);

                mProgressPercent.setText(result+ "%");

                

                if(mProgressbar.getProgress()==mProgressbar.getMax()){

                	Toast.makeText(DownloadActivity.this, "文件更新中", Toast.LENGTH_SHORT).show();
                	String parentPath=mFile.toString();
                	String fileName=mURL.substring(mURL.lastIndexOf("/")+1)+"";
                	String path=parentPath+File.separator+fileName;
                	Log.e("path",path);
                	Intent intent=new Intent(Intent.ACTION_VIEW);
                	intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
                	startActivity(intent);
                }

                break;

            case -1:

                Toast.makeText(DownloadActivity.this, R.string.error, 1).show();

                break;

            }

        }

    };

    

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        //super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download);
        mBack = (ImageView)findViewById(R.id.back);
        mBack.setVisibility(View.VISIBLE);
        mBack.setOnClickListener(this);
        mTitle = (TextView)findViewById(R.id.title);
        mTitle.setText("鏇存柊鐗堟湰");
        mProgressbar = (ProgressBar) this.findViewById(R.id.progressBar);
        mProgressPercent = (TextView) this.findViewById(R.id.progressPercent);


      

                if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                	mFile=new  File(Environment.getExternalStorageDirectory()+File.separator+"zkguanjia");
                    download(mURL,mFile);

                }else{

                    Toast.makeText(DownloadActivity.this, R.string.sdcarderror, Toast.LENGTH_LONG).show();

                }
        super.onCreate(savedInstanceState);

    }

    

      /**

       * 涓荤嚎绋�(UI绾跨▼)

       * 瀵逛簬鏄剧ず鎺т欢鐨勭晫闈㈡洿鏂板彧鏄敱UI绾跨▼璐熻矗锛屽鏋滄槸鍦ㄩ潪UI绾跨▼鏇存柊鎺т欢鐨勫睘鎬у�硷紝鏇存柊鍚庣殑鏄剧ず鐣岄潰涓嶄細鍙嶆槧鍒板睆骞曚笂

       * @param path

       * @param savedir
       * 

       */

    private void download(final String path, final File savedir) {

        new Thread(new Runnable() {            

            @Override

            public void run() {

                FileDownloader loader = new FileDownloader(DownloadActivity.this, path, savedir, 10);

                mProgressbar.setMax(loader.getFileSize());//璁剧疆杩涘害鏉＄殑鏈�澶у埢搴︿负鏂囦欢鐨勯暱搴�

                

                try {

                    loader.download(new DownloadProgressListener() {

                        @Override

                        public void onDownloadSize(int size) {//瀹炴椂鑾风煡鏂囦欢宸茬粡涓嬭浇鐨勬暟鎹暱搴�
                            Message msg = new Message();

                            msg.what = 1;

                            msg.getData().putInt("size", size);

                            handler.sendMessage(msg);//鍙戦�佹秷鎭�

                        }

                    });

                } catch (Exception e) {

                    handler.obtainMessage(-1).sendToTarget();

                }

            }

        }).start();

    }
    @Override
    public void onClick(View arg0) {
    	// TODO Auto-generated method stub
    	switch (arg0.getId()) {
		case R.id.btn_title_back:
			this.finish();
			break;

		default:
			break;
		}
    	
    }
}
